<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('menu')){
    function menu($menu_id,$class=null,$current_url=null,$selected_class="selected"){
        $ci = get_instance();
        $ci->load->model('Menu_item_model');
        $data = $ci->Menu_item_model->getMenuTree($menu_id);        
        
        $lastItem= count($data);
        $currenLevel = 1;
        $str = "<ul id='$class'>";
        $first=true;
        foreach ($data as $item){
            if($item['depth'] <= $currenLevel && !$first) $str .= "</li>";
	    if($item['depth'] < $currenLevel && !$first) { // exit child
                for($x = 0; $x < $currenLevel - $item['depth']; $x++){
                 $str .= "</ul></li>";
                }
            }
            if($item['depth'] > $currenLevel){ // has child
	        $str .= '<ul>';
	    }
            if (end($data) == $item) $theLast = ' last ';
            else $theLast = " ";
            $str .= "<li class='$theLast'> <a href=\"".  base_url().((!empty($item['link']))?("{$item['link']}.html"):null)."\" title=\"{$item['title']}\">{$item['title']}</a>\r\n";
            $currenLevel = $item['depth'];
            $first = false;
        }
        $str .= "</li></ul>";
        return $str;
    }
}
if ( ! function_exists('menu_by_language')){
    function menu_by_language($title,$language,$class=null,$current_url=null,$selected_class="selected"){
        $ci = get_instance();
        $ci->load->model('Menu_item_model');
        $data = $ci->Menu_item_model->getMenuTreeByLanguage($title,$language);        
        $lastItem= count($data);
        $currenLevel = 1;
        $str = '<ul>';
        $first=true;
        foreach ($data as $item){
            if($item['depth'] <= $currenLevel && !$first) $str .= "</li>";
	    if($item['depth'] < $currenLevel && !$first) { // exit child
                for($x = 0; $x < $currenLevel - $item['depth']; $x++){
                 $str .= "</ul></li>";
                }
            }
            if($item['depth'] > $currenLevel){ // has child
	        $str .= '<ul>';
	    }
            $str .= "<li> <a class=\"$class".(($current_url==$item['link'])?$selected_class:null)."\" href=\"".  base_url().((!empty($item['link']))?("{$item['link']}.html"):null)."\" title=\"{$item['title']}\"><span>{$item['title']}</span></a>\r\n";
            $currenLevel = $item['depth'];
            $first = false;
        }
        $str .= "</li></ul>";
        return $str;
    }
}
if ( ! function_exists('menuarray')){
    function menuarray($menu_id,$current_url=null){
        $ci = get_instance();
        $ci->load->model('Menu_item_model');
        $data = $ci->Menu_item_model->getMenuTree($menu_id);
        return $data;
    }
}

if ( ! function_exists('main_menu')){
      function main_menu($menu_id,$class=null,$current_url=null,$selected_class="selected"){
        $ci = get_instance();
        $ci->load->model('Menu_item_model');
        $data = $ci->Menu_item_model->getMenuTree($menu_id);        
        $lastItem= count($data);
        $currenLevel = 1;
        $str = "<ul id='$class'>";
        $first=true;
        createProductMenu($data);
        foreach ($data as $item){
            if($item['depth'] <= $currenLevel && !$first) $str .= "</li>";
	    if($item['depth'] < $currenLevel && !$first) { // exit child
                for($x = 0; $x < $currenLevel - $item['depth']; $x++){
                 $str .= "</ul></li>";
                }
            }
            if($item['depth'] > $currenLevel){ // has child
	        $str .= '<ul class="subNavi" style="display:none">';
	    }
            $parentCss = $item['depth'] == 1 ? 'parent' : '';
            if (end($data) == $item) $theLast = ' last ';
            else $theLast = "";
            $str .= "<li id='li_menu_{$item['link']}' class='{$parentCss}{$theLast}'> <a href=\"".  base_url().((!empty($item['link']))?("{$item['link']}.html"):null)."\" title=\"{$item['title']}\">{$item['title']}</a>\r\n";
            $currenLevel = $item['depth'];
            $first = false;
        }
        $str .= "</li></ul>";
        return $str;
    }
}
function createProductMenu(&$menu){
    $result = array();
    $tmp = $menu; 
    foreach ($tmp as $key => $item){
        $result[] = $item;
        unset($tmp[$key]);
        if ($item['link'] == 'products'){
            break;
        }
    }
    
    $ci = get_instance();
    $ci->load->model('cate_model');
    /* @var $catOb cate_model */
    $catOb = $ci->cate_model;
    
    $cat = $catOb->createMenuArray();
    
    foreach ($cat as $catItem){
        $result[] = $catItem;
    }
    
    foreach ($tmp as $key => $item){
        $result[] = $item;
        unset($tmp[$key]);
        if ($item['link'] == 'products'){
            break;
        }
    }
    
   $menu = $result;   
}
if ( !function_exists('menu_sitemap')){
    function menu_sitemap($menu_id,$class=null,$current_url=null,$selected_class="selected"){
        $ci = get_instance();
        $ci->load->model('Menu_item_model');
        $data = $ci->Menu_item_model->getMenuTree($menu_id);        
        createProductMenu($data);
        $lastItem= count($data);
        $currenLevel = 1;
        $str = "<ul id='$class'>";
        $first=true;
        foreach ($data as $item){
            if($item['depth'] <= $currenLevel && !$first) $str .= "</li>";
	    if($item['depth'] < $currenLevel && !$first) { // exit child
                for($x = 0; $x < $currenLevel - $item['depth']; $x++){
                 $str .= "</ul></li>";
                }
            }
            if($item['depth'] > $currenLevel){ // has child
	        $str .= '<ul>';
	    }
            if (end($data) == $item) $theLast = ' last ';
            else $theLast = " ";
            $str .= "<li class='$theLast'> <a href=\"".  base_url().((!empty($item['link']))?("{$item['link']}.html"):null)."\" title=\"{$item['title']}\">{$item['title']}</a>\r\n";
            $currenLevel = $item['depth'];
            $first = false;
        }
        $str .= "</li></ul>";
        return $str;
    }
}
?>
